package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.BusinessStatement;
import cn.wolfcode.car.business.mapper.BusinessStatementMapper;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.query.BusinessStatementQuery;
import cn.wolfcode.car.business.service.IBusinessStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class BusinessStatementServiceImpl implements IBusinessStatementService {

    @Autowired
    private BusinessStatementMapper businessStatementMapper;
    @Autowired
    private StatementItemMapper statementItemMapper;

    //分页+过滤
    public TablePageInfo<BusinessStatement> query(BusinessStatementQuery qo) {
        PageHelper.startPage(qo.getPageNum(),qo.getPageSize());
        List<BusinessStatement> businessStatements=businessStatementMapper.selectForList(qo);
        return new TablePageInfo<>(businessStatements);
    }

    @Override
    public BusinessStatement get(Long id) {
        return businessStatementMapper.selectByPrimaryKey(id);
    }

    //结算单添加
    public void save(BusinessStatement businessStatement) {
        //注意补充必要属性：结算单状态、创建时间
        businessStatement.setStatus(BusinessStatement.STATUS_CONSUME);
        businessStatement.setCreateTime(new Date());
        businessStatementMapper.insert(businessStatement);
    }

    @Override
    public void update(BusinessStatement businessStatement) {
        businessStatementMapper.updateByPrimaryKey(businessStatement);
    }

    @Override
    public void deleteBatch(String ids) {

    }


    @Override
    public List<BusinessStatement> list() {
        return businessStatementMapper.selectAll();
    }

    //在结算时，更新结算单信息
    public void updateStatement(Long statementId, BigDecimal totalAmount, BigDecimal totalQuantity, BigDecimal discountPrice, Date paytime, Long payeeId) {
        businessStatementMapper.updateStatement(statementId,totalAmount,totalQuantity,
                                discountPrice,paytime,payeeId);
    }

    //修改结算单的消费状态
    public void updateStatus(Long id, Integer statusPaid) {
        businessStatementMapper.updateStatus(id, statusPaid);
    }

    //硬删除:删除结算单之前，还需要删除关联的结算单明细
    public void deleteStatement(Long id) {
        statementItemMapper.deleteByStatementId(id);//删除关联的结算单明细
        businessStatementMapper.deleteByPrimaryKey(id);//删除结算单
    }

}
